home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / POLYLINE / IPOLYLIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  22KB  |  1,003 lines

  1. /*
  2.  * IPOLYLIN.CPP
  3.  * Modifications for Chapter 5.
  4.  *
  5.  * Implementation of the IPolyline interface that we expose on the
  6.  * CPolyline object.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "polyline.h"
  19.  
  20.  
  21. /*
  22.  * CImpIPolyline:CImpIPolyline
  23.  * CImpIPolyline::~CImpIPolyline
  24.  *
  25.  * Constructor Parameters:
  26.  *  pObj            LPVOID pointing to the object we live in.
  27.  *  punkOuter       LPUNKNOWN of the controlling unknown.
  28.  */
  29.  
  30. CImpIPolyline::CImpIPolyline(LPVOID pObj, LPUNKNOWN punkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pObj=pObj;
  34.     m_punkOuter=punkOuter;
  35.     return;
  36.     }
  37.  
  38.  
  39. CImpIPolyline::~CImpIPolyline(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CImpIPolyline::QueryInterface
  49.  * CImpIPolyline::AddRef
  50.  * CImpIPolyline::Release
  51.  *
  52.  * Purpose:
  53.  *  Standard set of IUnknown members for this interface
  54.  */
  55.  
  56. STDMETHODIMP CImpIPolyline::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  57.     {
  58.     return m_punkOuter->QueryInterface(riid, ppv);
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIPolyline::AddRef(void)
  62.     {
  63.     ++m_cRef;
  64.     return m_punkOuter->AddRef();
  65.     }
  66.  
  67. STDMETHODIMP_(ULONG) CImpIPolyline::Release(void)
  68.     {
  69.     --m_cRef;
  70.     return m_punkOuter->Release();
  71.     }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /*
  78.  * CImpIPolyline::Init
  79.  *
  80.  * Purpose:
  81.  *  Instantiates a polyline window within a given parent.  The
  82.  *  parent may be a main application window, could be an MDI child
  83.  *  window. We really do not care.
  84.  *
  85.  * Parameters:
  86.  *  hWndParent      HWND of the parent of this window
  87.  *  pRect           LPRECT that this window should occupy
  88.  *  dwStyle         DWORD containing the window's style flags
  89.  *  uID             UINT ID to associate with this window
  90.  *
  91.  * Return Value:
  92.  *  HRESULT         NOERROR if successful, otherwise E_OUTOFMEMORY
  93.  */
  94.  
  95. STDMETHODIMP CImpIPolyline::Init(HWND hWndParent, LPRECT pRect
  96.     , DWORD dwStyle, UINT uID)
  97.     {
  98.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  99.     SCODE           sc;
  100.  
  101.     pObj->m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPOLYLINE
  102.         , SZCLASSPOLYLINE, dwStyle, pRect->left, pRect->top
  103.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  104.         , hWndParent, (HMENU)uID, pObj->m_hInst, (LPVOID)pObj);
  105.  
  106.  
  107.     sc=(NULL!=pObj->m_hWnd) ? S_OK : E_OUTOFMEMORY;
  108.     return ResultFromScode(sc);
  109.     }
  110.  
  111.  
  112.  
  113.  
  114. //CHAPTER5MOD
  115. /*
  116.  * ::ReadFromFile and ::WriteToFile are replaced by the IPersistStorage
  117.  * implementation.  We no longer read from files, instead we use
  118.  * storages.
  119.  */
  120. //End CHAPTER5MOD
  121.  
  122.  
  123.  
  124.  
  125. /*
  126.  * CImpIPolyline::DataSet
  127.  *
  128.  * Purpose:
  129.  *  Sets the current data in this Polyline to a given structure.
  130.  *
  131.  * Parameters:
  132.  *  pplIn           LPPOLYLINEDATA to initialize to.
  133.  *  fSizeToData     BOOL indicating if we're to size to the data or scale it.
  134.  *  fNotify         BOOL indicating if we're to send an advise on this change.
  135.  *
  136.  * Return Value:
  137.  *  HRESULT         NOERROR if successful, otherwise a POLYLINE_E_ value.
  138.  */
  139.  
  140. STDMETHODIMP CImpIPolyline::DataSet(LPPOLYLINEDATA pplIn, BOOL fSizeToData
  141.     , BOOL fNotify)
  142.     {
  143.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  144.     LPPOLYLINEDATA  ppl=&pObj->m_pl;
  145.     RECT            rc;
  146.  
  147.     /*
  148.      * Copy the structure in pplIn and repaint to reflect the new point
  149.      * set.  Note that unlike the RectSet message, we do no scaling,
  150.      * assuming that the rect in the structure is appropriate for the data.
  151.      */
  152.  
  153.     if (NULL==pplIn)
  154.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  155.  
  156.     //Preserve the old rectangle, then copy
  157.     rc=ppl->rc;
  158.     *ppl=*pplIn;
  159.  
  160.     //Inform our parent of the data change
  161.     if (NULL!=pObj->m_pAdv)
  162.         {
  163.         pObj->m_fDirty=TRUE;
  164.         pObj->m_pAdv->OnDataChange();
  165.         }
  166.  
  167.     /*
  168.      * If we're scaling the window to fit the data, then use
  169.      * RectSet passing our current rectangle as the new one.
  170.      * That makes sure that the data won't change but that the
  171.      * window is resized.
  172.      */
  173.  
  174.     if (fSizeToData)
  175.         {
  176.         POINT       pt;
  177.  
  178.         /*
  179.          * Get our offset in the parent window so we can RectSet
  180.          * to the right place since RectSet expects rectangle in
  181.          * parent coordinates and we get it in client coordinates.
  182.          */
  183.         GetWindowRect(pObj->m_hWnd, &rc);
  184.         pt.x=rc.left;
  185.         pt.y=rc.top;
  186.         ScreenToClient(GetParent(pObj->m_hWnd), &pt);
  187.         rc=ppl->rc;
  188.         OffsetRect(&rc, pt.x, pt.y);
  189.  
  190.         //This will also cause a repaint.
  191.         RectSet(&rc, fNotify);
  192.         }
  193.     else
  194.         {
  195.         //Make sure we're updated.
  196.         InvalidateRect(pObj->m_hWnd, NULL, TRUE);
  197.         UpdateWindow(pObj->m_hWnd);
  198.         }
  199.  
  200.     return NOERROR;
  201.     }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. /*
  210.  * CImpIPolyline::DataGet
  211.  *
  212.  * Purpose:
  213.  *  Retrieves the Polyline's current data.
  214.  *
  215.  * Parameters:
  216.  *  pplIn           LPPOLYLINEDATA into which we copy the data.
  217.  *
  218.  * Return Value:
  219.  *  HRESULT         NOERROR if successful, otherwise a POLYLINE_E_ value.
  220.  */
  221.  
  222. STDMETHODIMP CImpIPolyline::DataGet(LPPOLYLINEDATA pplIn)
  223.     {
  224.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  225.  
  226.     if (NULL==pplIn)
  227.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  228.  
  229.     *pplIn=pObj->m_pl;
  230.     return NOERROR;
  231.     }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /*
  240.  * CImpIPolyline::DataSetMem
  241.  *
  242.  * Purpose:
  243.  *  Sets the Polyline's data using a global memory handle
  244.  *  instead of a pointer.
  245.  *
  246.  * Parameters:
  247.  *  hMem            HGLOBAL containing the data.
  248.  *  fFree           BOOL indicating if we're to free the data.  The memory
  249.  *                  will be freed regardless of any error returned from here.
  250.  *  fSizeToData     BOOL indicating if we're to size to the data or scale it.
  251.  *  fNotify         BOOL indicating if we're to send an advise on this change.
  252.  *
  253.  * Return Value:
  254.  *  HRESULT         NOERROR if successful, otherwise a POLYLINE_E_ value.
  255.  */
  256.  
  257. STDMETHODIMP CImpIPolyline::DataSetMem(HGLOBAL hMem, BOOL fFree
  258.     , BOOL fSizeToData, BOOL fNotify)
  259.     {
  260.     LPPOLYLINEDATA  ppl;
  261.     HRESULT         hr=ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  262.  
  263.     if (NULL!=hMem)
  264.         {
  265.         ppl=(LPPOLYLINEDATA)GlobalLock(hMem);
  266.  
  267.         hr=DataSet(ppl, fSizeToData, fNotify);
  268.  
  269.         GlobalUnlock(hMem);
  270.  
  271.         if (fFree)
  272.             GlobalFree(hMem);
  273.         }
  274.  
  275.     return hr;
  276.     }
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284. /*
  285.  * CImpIPolyline::DataGetMem
  286.  *
  287.  * Purpose:
  288.  *  Retrieves the Polyline's data in a global memory handle.
  289.  *
  290.  * Parameters:
  291.  *  phMem           HGLOBAL FAR * in which to store the handle.
  292.  *
  293.  * Return Value:
  294.  *  HRESULT         NOERROR if successful, otherwise a POLYLINE_E_ value.
  295.  */
  296.  
  297. STDMETHODIMP CImpIPolyline::DataGetMem(HGLOBAL FAR *phMem)
  298.     {
  299.     HGLOBAL         hMem;
  300.     LPPOLYLINEDATA  ppl;
  301.     HRESULT         hr=ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  302.  
  303.     if (NULL==phMem)
  304.         return ResultFromScode(POLYLINE_E_INVALIDPOINTER);
  305.  
  306.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, CBPOLYLINEDATA);
  307.  
  308.     if (NULL!=hMem)
  309.         {
  310.         ppl=(LPPOLYLINEDATA)GlobalLock(hMem);
  311.         hr=DataGet(ppl);
  312.  
  313.         GlobalUnlock(hMem);
  314.  
  315.         if (FAILED(hr))
  316.             {
  317.             GlobalFree(hMem);
  318.             hMem=NULL;
  319.             }
  320.         }
  321.  
  322.     *phMem=hMem;
  323.     return hr;
  324.     }
  325.  
  326.  
  327.  
  328.  
  329. /*
  330.  * CImpIPolyline::RenderBitmap
  331.  *
  332.  * Purpose:
  333.  *  Creates a bitmap image of the current Polyline.
  334.  *
  335.  * Parameters:
  336.  *  phBmp           HBITMAP FAR * in which to return the bitmap.
  337.  *
  338.  * Return Value:
  339.  *  HRESULT         NOERROR if successful, otherwise a POLYLINE_E_ value.
  340.  */
  341.  
  342. STDMETHODIMP CImpIPolyline::RenderBitmap(HBITMAP FAR *phBmp)
  343.     {
  344.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  345.     HDC             hDC;
  346.     HDC             hMemDC;
  347.     HBITMAP         hBmp;
  348.     RECT            rc;
  349.     HGDIOBJ         hObj;
  350.  
  351.     if (NULL==phBmp)
  352.         retu